package com.fan.context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FineServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //方法3：使用ServletContext获取web里面的资源的真实路径
        //ServletContext servletContext = getServletContext();
       // String realPath = servletContext.getRealPath("mm.png");
        //System.out.println(realPath);
        //InputStream resourceAsStream = servletContext.getResourceAsStream("mm.png");
        //System.out.println(resourceAsStream);
        //方法1：使用fileInputStream，此时使用磁盘路径
        //FileInputStream fileInputStream = new FileInputStream("C:\\Users\\29525\\Desktop\\mm.png");
        //System.out.println(fileInputStream);
        //方法2：使用ClassLoader将文件转换成流
        InputStream resourceAsStream = FineServlet.class.getClassLoader().getResourceAsStream("../../mm.png");
        System.out.println(resourceAsStream);
    }
}
